/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PageMenusImp : PageImpBaseClass
    {
        ListView listviewChosenMenus;
        ListView listviewAvailableMenus;
        ListView listviewChosenProductGroups;
        ListView listviewAvailableProductGroups;
        ComboBox comboBoxGroupsName;
        ComboBox comboBoxExistingMenus;

        Button buttonAddSelectedMenus;
        Button buttonRemoveSelectedMenus;
        Button buttonAddSelectedProductGroups;
        Button buttonRemoveSelectedProductGroups;
        Button buttonSetMenuTime;
        Button buttonAssignDefaultProductGroup;
        DbLinkedCheckBox cbM;
        DbLinkedCheckBox cbT;
        DbLinkedCheckBox cbW;
        DbLinkedCheckBox cbTh;
        DbLinkedCheckBox cbF;
        DbLinkedCheckBox cbS;
        DbLinkedCheckBox cbSu;

        ComboBox cbMenus;

        /// Constructor
        /// </summary>
        public PageMenusImp(ListView listviewChosenMenus,
                            ListView listviewAvailableMenus,
                            ListView listviewChosenProductGroups,
                            ListView listviewAvailableProductGroups,
                            UIElementCollection uielementsChildren,
                            ComboBox comboBoxGroupsName,
                            ComboBox comboBoxExistingMenus,
                            DbLinkedTextBox tbMenuName,
                            DbLinkedTextBox tbStartTime,
                            DbLinkedTextBox tbEndTime,
                            DbLinkedTextBox tbMenuGroupName,
                            Button buttonAddSelectedMenus,
                            Button buttonRemoveSelectedMenus,
                            Button buttonAddSelectedProductGroups,
                            Button buttonRemoveSelectedProductGroups,
                            Button buttonSetMenuTime,
                            Button bAssignDefaultProductGroup,
                            GroupBox menuGroupsGB, GroupBox menusGB,
                            DbLinkedCheckBox cbM,
                            DbLinkedCheckBox cbT,
                            DbLinkedCheckBox cbW,
                            DbLinkedCheckBox cbTh,
                            DbLinkedCheckBox cbF,
                            DbLinkedCheckBox cbS,
                            DbLinkedCheckBox cbSu
            )
            : base()
        {
            this.ButtonNumber = "11";
            this.ButtonLabelText = "Menus";
            mButtonDeleteEntityName = "Delete Menu";
            mButtonNewEntityName = "New Menu";
            mButtonNewGroupName = "New Menu Group";
            mButtonDeleteGroupName = "Delete Menu Group";

            RegisterSaveMeGroupBoxes(menuGroupsGB, menusGB);
            cbMenus = comboBoxExistingMenus;

            this.cbM = cbM;
            this.cbT = cbT;
            this.cbW = cbW;
            this.cbTh = cbTh;
            this.cbF = cbF;
            this.cbS = cbS;
            this.cbSu = cbSu;

            cbM.Checked += new RoutedEventHandler(cbday_Checked);
            cbM.Unchecked += new RoutedEventHandler(cbday_UnChecked);
            cbT.Checked += new RoutedEventHandler(cbday_Checked);
            cbT.Unchecked += new RoutedEventHandler(cbday_UnChecked);
            cbW.Checked += new RoutedEventHandler(cbday_Checked);
            cbW.Unchecked += new RoutedEventHandler(cbday_UnChecked);
            cbTh.Checked += new RoutedEventHandler(cbday_Checked);
            cbTh.Unchecked += new RoutedEventHandler(cbday_UnChecked);
            cbF.Checked += new RoutedEventHandler(cbday_Checked);
            cbF.Unchecked += new RoutedEventHandler(cbday_UnChecked);
            cbS.Checked += new RoutedEventHandler(cbday_Checked);
            cbS.Unchecked += new RoutedEventHandler(cbday_UnChecked);
            cbSu.Checked += new RoutedEventHandler(cbday_Checked);
            cbSu.Unchecked += new RoutedEventHandler(cbday_UnChecked);


            this.listviewAvailableMenus = listviewAvailableMenus;
            this.listviewChosenMenus = listviewChosenMenus;
            this.listviewChosenProductGroups = listviewChosenProductGroups;
            this.listviewAvailableProductGroups = listviewAvailableProductGroups;

            this.comboBoxExistingMenus = comboBoxExistingMenus;
            this.comboBoxGroupsName = comboBoxGroupsName;

            this.buttonAddSelectedMenus = buttonAddSelectedMenus;
            this.buttonRemoveSelectedMenus = buttonRemoveSelectedMenus;
            this.buttonAddSelectedProductGroups = buttonAddSelectedProductGroups;
            this.buttonRemoveSelectedProductGroups = buttonRemoveSelectedProductGroups;
            this.buttonSetMenuTime = buttonSetMenuTime;
            this.buttonAssignDefaultProductGroup = bAssignDefaultProductGroup;

            mTextBoxesList.Add(tbMenuName);
            mTextBoxesList.Add(tbStartTime);
            mTextBoxesList.Add(tbEndTime);
            mTextBoxesList.Add(tbMenuGroupName);

            RegisterListview(listviewAvailableMenus);
            RegisterListview(listviewChosenMenus);
            RegisterListview(listviewChosenProductGroups);
            RegisterListview(listviewAvailableProductGroups);

            RegisterUiElementsList(uielementsChildren);

            ListofComboboxes.Add(comboBoxGroupsName);
            ListofComboboxes.Add(comboBoxExistingMenus);

            listviewAvailableMenus.SelectionChanged +=
                new SelectionChangedEventHandler(listviewMenus_SelectionChanged_NOTHROW);
            listviewChosenMenus.SelectionChanged +=
                new SelectionChangedEventHandler(listviewMenus_SelectionChanged_NOTHROW);
            listviewAvailableProductGroups.SelectionChanged +=
                new SelectionChangedEventHandler(listviewMenuProductGroups_SelectionChanged_NOTHROW);
            listviewChosenProductGroups.SelectionChanged +=
                new SelectionChangedEventHandler(listviewMenuProductGroups_SelectionChanged_NOTHROW);

            mListOfControlsToHideInNonEditMode.Add(buttonAddSelectedMenus);
            mListOfControlsToHideInNonEditMode.Add(buttonRemoveSelectedMenus);
            mListOfControlsToHideInNonEditMode.Add(buttonAddSelectedProductGroups);
            mListOfControlsToHideInNonEditMode.Add(buttonRemoveSelectedProductGroups);
            mListOfControlsToHideInNonEditMode.Add(buttonSetMenuTime);
            mListOfControlsToHideInNonEditMode.Add(buttonAssignDefaultProductGroup);

            buttonAddSelectedMenus.Click += new RoutedEventHandler(buttonAddSelectedMenus_Click);
            buttonRemoveSelectedMenus.Click += new RoutedEventHandler(buttonRemSelectedMenus_Click);
            buttonAddSelectedProductGroups.Click += new RoutedEventHandler(buttonAddSelectedProductGroups_Click);
            buttonRemoveSelectedProductGroups.Click += new RoutedEventHandler(buttonRemoveSelectedProductGroups_Click);
            buttonAssignDefaultProductGroup.Click += new RoutedEventHandler(buttonAssignDefaultProductGroup_Click);
            buttonSetMenuTime.Click += new RoutedEventHandler(buttonSetMenuTime_Click);
            ///If the selection changes lock the form down.
            comboBoxGroupsName.SelectionChanged += new SelectionChangedEventHandler(comboBox_SelectionChanged);
            comboBoxExistingMenus.SelectionChanged += new SelectionChangedEventHandler(comboBox_SelectionChanged);


            mEntityAddDeleteLoggingEnabled = true;

        }

        public static bool CanRunMenuToday(MENU m)
        {
            bool canRun = false;
            try
            {
                short mask = 0;
                if (m.DaysToRunFlags != null)
                {
                    mask = (short)m.DaysToRunFlags;
                }
                else
                {
                    mask = 127;
                }
                DateTime rightNow = DateTime.Now;
                DayOfWeek dow = rightNow.DayOfWeek;

                if (dow == DayOfWeek.Monday && ((mask & 1) == 1))
                {
                    canRun = true;
                }
                else if (dow == DayOfWeek.Tuesday && ((mask & 2) == 2))
                {
                    canRun = true;
                }
                else if (dow == DayOfWeek.Wednesday && ((mask & 4) == 4))
                {
                    canRun = true;
                }
                else if (dow == DayOfWeek.Thursday && ((mask & 8) == 8))
                {
                    canRun = true;
                }
                else if (dow == DayOfWeek.Friday && ((mask & 16) == 16))
                {
                    canRun = true;
                }
                else if (dow == DayOfWeek.Saturday && ((mask & 32) == 32))
                {
                    canRun = true;
                }
                else if (dow == DayOfWeek.Sunday && ((mask & 64) == 64))
                {
                    canRun = true;
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
            }
            return canRun;
        }

        private void UpdateDaysOfWeek()
        {
            try
            {
                MENU m = cbMenus.SelectedItem as MENU;

                if (m != null && m.DaysToRunFlags != null)
                {
                    short mask = 0;
                    if (m.DaysToRunFlags != null)
                    {
                        mask = (short)m.DaysToRunFlags;
                    }
                    else
                    {
                        m.DaysToRunFlags = 127;
                        mask = 127;
                    }

                    ///Monday
                    if ((mask & 1) == 1)
                    {
                        cbM.IsChecked = true;
                    }
                    else
                    {
                        cbM.IsChecked = false;
                    }
                    ///Tuesday
                    if ((mask & 2) == 2)
                    {
                        cbT.IsChecked = true;
                    }
                    else
                    {
                        cbT.IsChecked = false;
                    }
                    ///Wednesday
                    if ((mask & 4) == 4)
                    {
                        cbW.IsChecked = true;
                    }
                    else
                    {
                        cbW.IsChecked = false;
                    }
                    ///Thursday
                    if ((mask & 8) == 8)
                    {
                        cbTh.IsChecked = true;
                    }
                    else
                    {
                        cbTh.IsChecked = false;
                    }
                    ///Friday
                    if ((mask & 16) == 16)
                    {
                        cbF.IsChecked = true;
                    }
                    else
                    {
                        cbF.IsChecked = false;
                    }
                    ///Saturday
                    if ((mask & 32) == 32)
                    {
                        cbS.IsChecked = true;
                    }
                    else
                    {
                        cbS.IsChecked = false;
                    }
                    ///Sunday
                    if ((mask & 64) == 64)
                    {
                        cbSu.IsChecked = true;
                    }
                    else
                    {
                        cbSu.IsChecked = false;
                    }
                }
                else
                {
                    cbM.IsChecked = false;
                    cbT.IsChecked = false;
                    cbW.IsChecked = false;
                    cbTh.IsChecked = false;
                    cbF.IsChecked = false;
                    cbS.IsChecked = false;
                    cbSu.IsChecked = false;
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
            }
        }

        void cbday_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                DbLinkedCheckBox cb = sender as DbLinkedCheckBox;
                MENU m = cbMenus.SelectedItem as MENU;
                if (cb != null && m != null)
                {
                    short flagValue = 0;
                    if (m.DaysToRunFlags != null)
                    {
                        flagValue = (short)m.DaysToRunFlags;
                    }

                    if (cb == cbM)
                    {
                        m.DaysToRunFlags = (short)(flagValue | 1);
                    }
                    else if (cb == cbT)
                    {
                        m.DaysToRunFlags = (short)(flagValue | 2);
                    }
                    else if (cb == cbW)
                    {
                        m.DaysToRunFlags = (short)(flagValue | 4);
                    }
                    else if (cb == cbTh)
                    {
                        m.DaysToRunFlags = (short)(flagValue | 8);
                    }
                    else if (cb == cbF)
                    {
                        m.DaysToRunFlags = (short)(flagValue | 16);
                    }
                    else if (cb == cbS)
                    {
                        m.DaysToRunFlags = (short)(flagValue | 32);
                    }
                    else if (cb == cbSu)
                    {
                        m.DaysToRunFlags = (short)(flagValue | 64);
                    }
                    Context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
            }
        }

        void cbday_UnChecked(object sender, RoutedEventArgs e)
        {
            try
            {
                DbLinkedCheckBox cb = sender as DbLinkedCheckBox;
                MENU m = cbMenus.SelectedItem as MENU;
                if (cb != null && m != null)
                {
                    short flagValue = 0;
                    if (m.DaysToRunFlags != null)
                    {
                        flagValue = (short)m.DaysToRunFlags;
                    }

                    if (cb == cbM)
                    {
                        m.DaysToRunFlags = (short)(flagValue & 126);
                    }
                    else if (cb == cbT)
                    {
                        m.DaysToRunFlags = (short)(flagValue & 125);
                    }
                    else if (cb == cbW)
                    {
                        m.DaysToRunFlags = (short)(flagValue & 123);
                    }
                    else if (cb == cbTh)
                    {
                        m.DaysToRunFlags = (short)(flagValue & 119);
                    }
                    else if (cb == cbF)
                    {
                        m.DaysToRunFlags = (short)(flagValue & 111);
                    }
                    else if (cb == cbS)
                    {
                        m.DaysToRunFlags = (short)(flagValue & 95);
                    }
                    else if (cb == cbSu)
                    {
                        m.DaysToRunFlags = (short)(flagValue & 63);
                    }
                    Context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logging.LogException(ex);
            }
        }

        void buttonAssignDefaultProductGroup_Click(object sender, RoutedEventArgs e)
        {
            MENU menu = comboBoxExistingMenus.SelectedItem as MENU;
            if (menu != null)
            {
                AssignDefaultProductGroup mb = new AssignDefaultProductGroup(Context, menu);
                mb.SetMessage("Please select from the list of available product groups to assign a default product group for this menu.");
                mb.InitChoices();
                mb.ShowDialog();
            }
        }

        void buttonSetMenuTime_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MENU selected = comboBoxExistingMenus.SelectedItem as MENU;
                if (selected != null)
                {
                    TimePicker dtp = new TimePicker(true);
                    dtp.ShowDialog();
                    if (TimePicker.LastResponse == UserResponse.YES)
                    {
                        selected.StartTime = TimePicker.LastStartSelectedDateTime;
                        selected.EndTime = TimePicker.LastEndSelectedDateTime;
                        Context.SaveChanges();
                    }
                }
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PLACEHOLDER, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        override public List<EntityObject> GetGroupObjectSet()
        {
            try
            {
                var q = from MG in Context.MENU_GROUPS where MG.IsDeleted == false select MG;
                if (q.Any())
                {
                    return new List<EntityObject>(q.ToList());
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        override public List<EntityObject> GetEntityObjectSet()
        {
            try
            {
                var q = from M in Context.MENUS where M.IsDeleted == false select M;
                if (q.Any())
                {
                    return new List<EntityObject>(q.ToList());
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateListViews();
            UpdateDaysOfWeek();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void listviewMenus_SelectionChanged_NOTHROW(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (this.IsEditableMode())
                {
                    ListView lv = sender as ListView;
                    if (lv == listviewAvailableMenus)
                    {
                        if (lv.SelectedItems.Count != 0)
                        {
                            buttonAddSelectedMenus.IsEnabled = true;
                        }
                        else
                        {
                            buttonAddSelectedMenus.IsEnabled = false;
                        }
                    }
                    else if (lv == listviewChosenMenus)
                    {
                        if (lv.SelectedItems.Count != 0)
                        {
                            buttonRemoveSelectedMenus.IsEnabled = true;
                        }
                        else
                        {
                            buttonRemoveSelectedMenus.IsEnabled = false;
                        }
                    }
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PLACEHOLDER, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void listviewMenuProductGroups_SelectionChanged_NOTHROW(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (this.IsEditableMode())
                {
                    ListView lv = sender as ListView;
                    if (lv == listviewAvailableProductGroups)
                    {
                        if (lv.SelectedItems.Count != 0)
                        {
                            buttonAddSelectedProductGroups.IsEnabled = true;
                        }
                        else
                        {
                            buttonAddSelectedProductGroups.IsEnabled = false;
                        }
                    }
                    else if (lv == listviewChosenProductGroups)
                    {
                        if (lv.SelectedItems.Count != 0)
                        {
                            buttonRemoveSelectedProductGroups.IsEnabled = true;
                        }
                        else
                        {
                            buttonRemoveSelectedProductGroups.IsEnabled = false;
                        }
                    }
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PLACEHOLDER, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void ResetFocus()
        {
            base.ResetFocus();
            buttonRemoveSelectedMenus.IsEnabled = false;
            buttonAddSelectedMenus.IsEnabled = false;
            buttonRemoveSelectedProductGroups.IsEnabled = false;
            buttonAddSelectedProductGroups.IsEnabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAddSelectedMenus_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MENU_GROUP mg = comboBoxGroupsName.SelectedItem as MENU_GROUP;
                if (mg != null)
                {
                    foreach (object menu in listviewAvailableMenus.SelectedItems)
                    {
                        mg.MENUS.Add(menu as MENU);
                    }
                    UpdateListViews();
                }
            }
            catch { }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonRemSelectedMenus_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MENU_GROUP mg = comboBoxGroupsName.SelectedItem as MENU_GROUP;
                if (mg != null)
                {
                    foreach (object menu in listviewChosenMenus.SelectedItems)
                    {
                        mg.MENUS.Remove(menu as MENU);
                    }
                }
                UpdateListViews();
            }
            catch { }
        }

        void buttonRemoveSelectedProductGroups_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MENU menu = comboBoxExistingMenus.SelectedItem as MENU;
                if (menu != null)
                {
                    foreach (object pg in listviewChosenProductGroups.SelectedItems)
                    {
                        menu.PRODUCT_GROUPS.Remove(pg as PRODUCT_GROUP);
                    }
                }
                UpdateListViews();
            }
            catch { }
        }
        void buttonAddSelectedProductGroups_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MENU menu = comboBoxExistingMenus.SelectedItem as MENU;
                if (menu != null)
                {
                    foreach (object pg in listviewAvailableProductGroups.SelectedItems)
                    {
                        menu.PRODUCT_GROUPS.Add(pg as PRODUCT_GROUP);
                    }
                    UpdateListViews();
                }
            }
            catch { }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateListViews()
        {
            MENU_GROUP mg = comboBoxGroupsName.SelectedItem as MENU_GROUP;
            if (mg != null)
            {
                List<MENU> availMenus = new List<MENU>();
                var qAvailable = from M in Context.MENUS
                                 where M.IsDeleted == false
                                 orderby M.Name
                                 select M;

                List<MENU> usedList = null;
                if (qAvailable.Any())
                {
                    usedList = mg.MENUS.ToList();
                    foreach (MENU m in qAvailable)
                    {
                        if (!usedList.Contains(m))
                        {
                            availMenus.Add(m);
                        }
                    }
                }

                listviewAvailableMenus.ItemsSource = availMenus;
                listviewChosenMenus.ItemsSource = usedList;
            }
            else
            {
                listviewAvailableMenus.ItemsSource = null;
                listviewChosenMenus.ItemsSource = null;
            }

            MENU selectedMenu = comboBoxExistingMenus.SelectedItem as MENU;
            if (selectedMenu != null)
            {
                List<PRODUCT_GROUP> availProdGroups = new List<PRODUCT_GROUP>();
                var qAvailable = from PG in Context.PRODUCT_GROUPS
                                 where PG.IsDeleted == false
                                 orderby PG.Name
                                 select PG;

                List<PRODUCT_GROUP> usedList = null;
                if (qAvailable.Any())
                {
                    usedList = selectedMenu.PRODUCT_GROUPS.ToList();
                    foreach (PRODUCT_GROUP fm in qAvailable)
                    {
                        if (!usedList.Contains(fm))
                        {
                            availProdGroups.Add(fm);
                        }
                    }
                }

                listviewAvailableProductGroups.ItemsSource = availProdGroups;
                listviewChosenProductGroups.ItemsSource = usedList;
            }
            else
            {
                listviewAvailableProductGroups.ItemsSource = null;
                listviewChosenProductGroups.ItemsSource = null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject GetSelectedGroupObject()
        {
            MENU_GROUP selected = comboBoxGroupsName.SelectedItem as MENU_GROUP;
            return selected;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject GetSelectedEntityObject()
        {
            MENU selected = comboBoxExistingMenus.SelectedItem as MENU;
            return selected;
        }

        /// <summary>
        /// 
        /// </summary>
        public override void MarkGroupDeleted()
        {
            MENU_GROUP selected = comboBoxGroupsName.SelectedItem as MENU_GROUP;
            if (selected != null)
            {
                selected.IsDeleted = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void MarkEntityDeleted()
        {
            MENU selected = comboBoxExistingMenus.SelectedItem as MENU;
            if (selected != null)
            {
                selected.IsDeleted = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        public override void UpdateUsersGroupSource()
        {
            try
            {
                ///Create the query object.
                var query = from MG in
                                Context.MENU_GROUPS
                            where MG.IsDeleted == false
                            orderby MG.Name
                            select MG;

                if (query.Any())
                {
                    List<MENU_GROUP> list = query.ToList();
                    comboBoxGroupsName.ItemsSource = list;
                    if (list != null && list.Count != 0)
                    {
                        comboBoxGroupsName.SelectedItem = list.First();
                    }
                    else
                    {
                        comboBoxGroupsName.ItemsSource = null;
                    }
                }
                else
                {
                    comboBoxGroupsName.ItemsSource = null;
                }
                UpdateListViews();
            }
            catch { }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        public override void UpdateUsersEntitySource()
        {
            try
            {
                ///Create the query object.
                var query = from MENU in
                                Context.MENUS
                            where MENU.IsDeleted == false
                            orderby MENU.Name
                            select MENU;

                if (query.Any())
                {
                    List<MENU> list = query.ToList();
                    comboBoxExistingMenus.ItemsSource = list;
                    if (list != null && list.Count != 0)
                    {
                        comboBoxExistingMenus.SelectedItem = list.First();
                    }
                    else
                    {
                        comboBoxExistingMenus.ItemsSource = null;
                    }
                }
                else
                {
                    comboBoxExistingMenus.ItemsSource = null;
                }
            }
            catch { }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject AddNewGroupObject()
        {
            MENU_GROUP newObject = null;
            try
            {
                var q = from MENU_GROUP in Context.MENU_GROUPS
                        where MENU_GROUP.Name.Equals("Not Assigned")
                        select MENU_GROUP;

                if (q.Count() == 0)
                {
                    newObject = new MENU_GROUP();
                    newObject.Name = "Not Assigned";
                    Context.MENU_GROUPS.AddObject(newObject);
                }
                else
                {
                    newObject = q.First();
                    if (newObject.IsDeleted)
                    {
                        newObject.IsDeleted = false;
                    }
                }
            }
            catch { }
            return newObject;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject AddNewEntityObject()
        {
            MENU newObject = null;
            try
            {
                var q = from MENU in Context.MENUS
                        where MENU.Name.Equals("Not Assigned")
                        select MENU;

                if (q.Count() == 0)
                {
                    newObject = new MENU();
                    newObject.Name = "Not Assigned";
                    newObject.StartTime = DateTime.Now;
                    newObject.EndTime = DateTime.Now;
                    newObject.DaysToRunFlags = 127;
                    Context.MENUS.AddObject(newObject);
                }
                else
                {
                    newObject = q.First();
                    if (newObject.IsDeleted)
                    {
                        newObject.IsDeleted = false;
                        newObject.StartTime = DateTime.Now;
                        newObject.EndTime = DateTime.Now;
                        newObject.DaysToRunFlags = 127;
                    }
                }
            }
            catch { }
            return newObject;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ComboBox GetGroupComboBox()
        {
            return comboBoxGroupsName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ComboBox GetEntityComboBox()
        {
            return comboBoxExistingMenus;
        }
    }
}
